package com.ruoyi.datam.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.datam.mapper.WiOrganBMapper;
import com.ruoyi.datam.service.IWiOrganBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.domain.base.datam.WiOrganB;

/**
 * 水管机构登记Service业务层处理
 *
 * @author hwd
 * @date 2023-02-15
 */
@Service
public class WiOrganBServiceImpl implements IWiOrganBService {

    private static final Logger log = LoggerFactory.getLogger(WiOrganBServiceImpl.class);

    @Autowired
    private WiOrganBMapper wiOrganBMapper;

    /**
     * 查询水管机构登记
     *
     * @param orgCd 水管机构登记主键
     * @return 水管机构登记
     */
    @Override
    public WiOrganB selectWiOrganBByOrgCd(String orgCd) {
        return wiOrganBMapper.selectWiOrganBByOrgCd(orgCd);
    }

    /**
     * 查询水管机构登记列表
     *
     * @param wiOrganB 水管机构登记
     * @return 水管机构登记
     */
    @Override
    public List<WiOrganB> selectWiOrganBList(WiOrganB wiOrganB) {
        if(wiOrganB.getOrgCd()!=null){
            HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(wiOrganB.getOrgCd());
            wiOrganB.setOrgCd(addvAndOrgCodeInfo.get("resCode")+"");
        }
        return wiOrganBMapper.selectWiOrganBList(wiOrganB);
    }

    /**
     * 新增水管机构登记
     *
     * @param wiOrganB 水管机构登记
     * @return 结果
     */
    @Override
    public int insertWiOrganB(WiOrganB wiOrganB) {
        //获取
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(wiOrganB.getParentOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        if (grade == 1){
            //查找下属水管机构
            resCode =  resCode + "%0000";
        }else if (grade == 2) {
            //查找下属水管机构
            resCode =resCode + "%00";
        }else if(grade ==3) {
            resCode =resCode+"%";
        }else {
            resCode = resCode;
        }
        wiOrganB.setOrgCd(resCode);
        Integer count= wiOrganBMapper.selectAllWiOrganBList(wiOrganB).size();
        String xh=count.toString();
        if(count<10){
            xh="0"+count;
        }
        if (grade==2){
            wiOrganB.setOrgLevel("3");
            wiOrganB.setOrgCd(addvAndOrgCodeInfo.get("resCode")+xh+"00");
        }else if (grade==3){
            wiOrganB.setOrgLevel("4");
            wiOrganB.setOrgCd(addvAndOrgCodeInfo.get("resCode")+xh);
        }else {
            return 0;
        }
        return wiOrganBMapper.insertWiOrganB(wiOrganB);
    }

    /**
     * 修改水管机构登记
     *
     * @param wiOrganB 水管机构登记
     * @return 结果
     */
    @Override
    public int updateWiOrganB(WiOrganB wiOrganB) {
        return wiOrganBMapper.updateWiOrganB(wiOrganB);
    }

    /**
     * 批量删除水管机构登记
     *
     * @param orgCds 需要删除的水管机构登记主键
     * @return 结果
     */
    @Override
    public int deleteWiOrganBByOrgCds(String[] orgCds) {
        return wiOrganBMapper.deleteWiOrganBByOrgCds(orgCds);
    }

    /**
     * 删除水管机构登记信息
     *
     * @param orgCd 水管机构登记主键
     * @return 结果
     */
    @Override
    public int deleteWiOrganBByOrgCd(String orgCd) {
        return wiOrganBMapper.deleteWiOrganBByOrgCd(orgCd);
    }

    /**
     * 获取树形机构列表
     *
     * @return
     */
    @Override
    public List<WiOrganB> treeList(WiOrganB wiOrganB) {
        List<WiOrganB> wiOrganBS = wiOrganBMapper.selectWiOrganBList(wiOrganB);
        String orgCdQuery;
        for (WiOrganB vo : wiOrganBS) {
            HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(vo.getOrgCd());
            orgCdQuery = (addvAndOrgCodeInfo.get("resCode") + "").substring(0, (addvAndOrgCodeInfo.get("resCode") + "").length() - 2);
//            WiOrganB wiOrganB = wiOrganBMapper.selectWiOrganBByOrgCd((orgCdQuery + "000000").substring(0, 13));
            String orgCd = (orgCdQuery + "000000").substring(0, 13);
            List<WiOrganB> collect = wiOrganBS.parallelStream().filter(vo2 -> vo2.getOrgCd().equals(orgCd)).collect(Collectors.toList());
            if (collect.size() > 0 && collect.get(0) != null) {
                vo.setParentOrgCd(collect.get(0).getOrgCd());
            }
        }
        return wiOrganBS;
    }
}
